<template>
  <div class="banner b2">
    <el-card shadow="hover" class="login">
      <p class="header" slot="header">
        <i class="icon-businesscard h"></i>登录
        <el-button type="text" style="float:right;padding: 5px 0;" @click="isPrompt = true">登录说明</el-button>
      </p>
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item class="margin-b" prop="user">
          <el-input v-model="form.user" placeholder="请输入账号">
            <i slot="prefix" class="icon-mine input-icon"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="margin-b" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码">
            <i slot="prefix" class="icon-lock input-icon"></i>
          </el-input>
        </el-form-item>
        <el-button
          type="primary"
          class="submit"
          plain
          style="width: 100%"
          :loading="loginFlag.is"
          @click="login">{{ loginFlag.is ? '验证用户信息...' : '登录' }}</el-button>
      </el-form>
    </el-card>
    <el-dialog
      class="dialog-width"
      title="温馨提示"
      :visible.sync="isPrompt"
      width="35%">
      <span v-text="promptData"></span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="isPrompt = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { CODE_OK } from '@/api/config'
import { isVisit } from '@/api/login'
import stroage from '@/common/js/storage'
import promptData from './data'

const LOGIN_STATE = {
  wait: 0,
  success: 1,
  error: 2
}

export default {
  name: 'Login',
  data() {
    const validateUser = (rule, value, callback) => {
      const state = this.loginFlag.user
      if (value === '') {
        return callback(new Error('用户名不能为空'))
      }

      switch (state) {
        case LOGIN_STATE.wait:
        case LOGIN_STATE.success:
          callback()
          break
        case LOGIN_STATE.error:
          callback(new Error('用户名不存在'))
      }
    }

    const validatePass = (rule, value, callback) => {
      const state = this.loginFlag.password
      if (value === '') {
        return callback(new Error('密码不能为空'))
      }

      switch (state) {
        case LOGIN_STATE.wait:
        case LOGIN_STATE.success:
          callback()
          break
        case LOGIN_STATE.error:
          callback(new Error('密码错误'))
      }
    }
    return {
      isPrompt: false,
      form: {
        user: '',
        password: ''
      },
      loginFlag: {
        is: false,
        user: LOGIN_STATE.wait,
        password: LOGIN_STATE.wait
      },
      rules: {
        user: [
          { validator: validateUser, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.showPrompt()
  },
  mounted() {

  },
  methods: {
    login() {
      this.$refs.form.validate((bool) => {
        if (!bool) {
          return
        }
        let loginFlag = this.loginFlag
        loginFlag.is = true

        setTimeout(() => {
          const resData = {
            account: this.form.user,
            password: this.form.password
          }

          isVisit(resData).then((res) => {
            const _data = res.data

            if (res.code === CODE_OK) {
              // 已过期
              if (_data.duration < 0) {
                this.$message.warning('该账号已经过了有效期！')
                return
              }

              loginFlag.user = _data.is_account ? LOGIN_STATE.success : LOGIN_STATE.error
              loginFlag.password = _data.is_password ? LOGIN_STATE.success : LOGIN_STATE.error
            }

            this.$refs.form.validate((b) => {
              if (b) {
                // 存储信息至sessionStorage
                stroage.session.set('user', {
                  name: _data.name,
                  welcomeMessage: false,
                  overdueMessage: false,
                  account: loginFlag.user,
                  duration: _data.duration,
                  dateLogin: _data.date_login,
                  dateLoginNew: new Date().getTime()
                })
                this.$router.push('/home')
              }
              loginFlag.user = LOGIN_STATE.wait
              loginFlag.password = LOGIN_STATE.wait
            })
          }).catch(() => {
            this.$message.error('未知错误，请联系管理员！')
          }).finally(() => {
            loginFlag.is = false
          })
        }, 1000)
      })
    },
    showPrompt() {
      const client = stroage.session.get('client')

      // 显示一次提示框
      if (!client || !client.promptMessage) {
        this.isPrompt = true
        stroage.session.set('client', Object.assign({}, client, {
          promptMessage: true
        }))
      }
    }
  },
  computed: {
    promptData() {
      return promptData
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~common/stylus/variable.styl'
@import '~common/stylus/mixin.styl'

.banner
  &.b2
    background-image: requireImg('banner-login.jpg')
  .header
    line-height: 24px
  .h
    float: left
    padding-right: 5px
    span
      font-size: 14px
      line-height: 24px
  .login
    width: 350px
    margin: 0 auto
    position: fixed
    top: 50%
    right: 200px
    transform: translateY(-60%)
    background-color: rgba(255, 255, 255, 0.9)
    .margin-b
      margin-bottom: 30px
  .input-icon
    font-size: 20px
    line-height: 40px

.submit
  width: 100%
  height: 60px

@media (max-width: $media-smail)
  .login
    width: 100% !important
    height: 100% !important
    top: 0 !important
    right: 0 !important
    transform: translateY(0) !important
    border-radius: 0 !important
    .margin-b
      margin-bottom: 22px !important
  .banner
    &.b2
      background: requireImg('m-banner-login.jpg') center center no-repeat !important
      background-size: 100% 100% !important
</style>
